﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

public partial class CustomerArchives_CustomerArchiveEdit : System.Web.UI.Page
{
    public string SP_PARAM14
    {
        get
        {
            Model.SYSPARAM sysparam = new Model.SYSPARAM();
            sysparam = new BLL.SYSPARAM().GetModel("007");
            return sysparam.SP_PARAM14;
        }
    }
    public string usercode = "";

    protected void Page_Load(object sender, EventArgs e)
    {

          usercode = Request["code"].ToString();
        if (!Page.IsPostBack)
        {
            Bind1();

            GetCustomerInfo();

            hdsysParam.Value = SP_PARAM14;
            if (!Check.CheckPermission("200500101", "00002"))
            {
                Button1.Enabled = false;
                Button2.Enabled = false;
            }
            else
            {
                Button1.Enabled = true;
                Button2.Enabled = true;
            }

            Model.V_CUSTOMER_AUTO model_auto = new BLL.V_CUSTOMER_AUTO().GetModel(usercode);
            if (model_auto.VC_STOP == 1)
            {
                Button1.Enabled = false;
                Button2.Enabled = false;
            }
            else
            {
                Button1.Enabled = true;
                Button2.Enabled = true;
            }
        }
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
        HasPermission = Check.CheckPermission("200500101", "00040");
    }



    //是否有权限

    public bool HasPermission = false;
    public string NoPermission = "******";





    private void GetCustomerInfo()
    {



        Model.V_CUSTOMER_AUTO model_auto = new BLL.V_CUSTOMER_AUTO().GetModel(usercode);

        Model.V_CUSTOMER model_customer = new BLL.V_CUSTOMER().GetModel(usercode);

        //DataSet v_customer_address = new BLL.V_CUSTOMER_ADDRESS().Address_Init(usercode);

        DataSet relativeInfo = new BLL.V_CUSTOMER().GetCustomerInfo(usercode);
        if (relativeInfo.Tables[0].Rows.Count > 0)
        {
            DataRow row=relativeInfo.Tables[0].Rows[0];
            txtAddress.Text = HasPermission ? row["DZ"].ToString() : NoPermission;
            
            txtEmail.Text = HasPermission ? row["EMAIL"].ToString() : NoPermission;
           

            lbCardNo.Text = row["CKM"].ToString();
            lbCardType.Text = row["CardType"].ToString();
            lbCardAmt.Text = row["XFJE"].ToString();
            lbMemberType.Text = row["CT_NAME"].ToString();
            lbPromiseAmt.Text = String.Format("{0:0.00}", row["BYZD8"]);
            lbShoppingQty.Text = row["XFSL"].ToString();
            lbCreditLevel.Text = row["BYZD12"].ToString();
            lbBalanceAmt.Text = String.Format("{0:0.00}", row["DQJE_1"]);
            lbMemberDiscount.Text = String.Format("{0:0.00}", row["ZK"]);
            lbJF.Text = String.Format("{0:0.00}", row["JFJE"]);
            lbCurrentJF.Text = row["DQJF"].ToString();
        }
        txtBZ.Text = model_customer.BZ;

        string strSex = string.Empty;
        switch (model_customer.SEX)
        {
            case "0":
                strSex = "0";
                break;
            case "1":

                strSex = "1";
                break;
  
        
            case "男":
                strSex = "0";
                break;
            case "女":
                strSex = "1";
                break;
            default:
                strSex = "0";
                break;
        }
        if (strSex == "0")
        {
            RadioButton1.Checked = true;
        }
        else
        {
            RadioButton2.Checked = true;
        }

        Label1.Text = usercode;
        if (model_auto.VC_STOP == 1)
        { Label1.Text = Label1.Text + "(该成员已被列入黑名单)"; }

        Label2.Text = model_customer.BYZD5;

        TextBox1.Text = model_customer.GKMC;


        try
        {
            TextBox2.Text = model_auto.CardNO;
        }
        catch {
            TextBox2.Text = "";
        }
        TextBox10.Text = model_customer.SR.ToShortDateString();


        TextBox4.Text = HasPermission ? model_customer.SJ : NoPermission;
       
        TextBox3.Text = HasPermission ? model_customer.YB : NoPermission;
        

        //电话和区号
        if (model_customer.DH.IndexOf("-") != -1)
        {
            string[] arr = model_customer.DH.Split('-');
            TextBox5.Text = HasPermission ? arr[0]: NoPermission;

            TextBox6.Text = HasPermission ? arr[1] : NoPermission;
            if (arr.Length == 3)
            {
                TextBox11.Text = HasPermission ? arr[2] : NoPermission;
            }
            
        }
        else
        {
            TextBox5.Text = HasPermission ?"021" : NoPermission;
            TextBox6.Text = HasPermission ? model_customer.DH : NoPermission;
            TextBox11.Text = HasPermission ? "" : NoPermission;
        }

        TextBox1.ReadOnly = !HasPermission;
        TextBox2.ReadOnly = !HasPermission;
        TextBox3.ReadOnly = !HasPermission;
        TextBox4.ReadOnly = !HasPermission;
        TextBox5.ReadOnly = !HasPermission;
        TextBox6.ReadOnly = !HasPermission;
        TextBox11.ReadOnly = !HasPermission;
        TextBox7.ReadOnly = !HasPermission;
        TextBox8.ReadOnly = !HasPermission;
        TextBox9.ReadOnly = !HasPermission; 
        txtAddress.ReadOnly = !HasPermission;
        txtEmail.ReadOnly = !HasPermission;
        txtBZ.ReadOnly = !HasPermission;
        DropDownList1.Enabled = HasPermission;
        DropDownList2.Enabled = HasPermission;
        DropDownList3.Enabled = HasPermission;
        DropDownList4.Enabled = HasPermission;
        RadioButton1.Enabled = HasPermission;
        RadioButton2.Enabled = HasPermission;
        //  qq,msn ,旺旺号
        try
        {
            TextBox7.Text = HasPermission ?model_auto.BYZD18.ToString(): NoPermission;
        }
        catch {
            TextBox7.Text = HasPermission ? "" : NoPermission;
        }
        try
        {
            TextBox8.Text = HasPermission ? model_auto.BYZD16 : NoPermission;
        }
        catch
        {
            TextBox8.Text =  HasPermission ?"": NoPermission;
        }
        try
        {
            TextBox9.Text =  HasPermission ?model_auto.BYZD17: NoPermission;
        }
        catch {
            TextBox9.Text = HasPermission ? "": NoPermission;
        }
        Label3.Text = new BLL.V_CUSTOMER().GetXinYu(3, usercode);
        try
        { 
            //model_auto.VC_PROVINCE=model_auto.VC_PROVINCE == string.Empty ? v_customer_address.Tables[1].Rows[0]["Provice"].ToString() : model_auto.VC_PROVINCE;

            DropDownList2.SelectedValue = model_auto.VC_PROVINCE;


            DropDownList3.DataTextField = "CityName";
            DropDownList3.DataValueField = "CityId";
            DropDownList3.DataSource = new BLL.City().GetAllList(model_auto.VC_PROVINCE);
            DropDownList3.DataBind();

            //model_auto.VC_CITY = model_auto.VC_CITY == string.Empty ? v_customer_address.Tables[1].Rows[0]["City"].ToString() : model_auto.VC_CITY;
            DropDownList3.SelectedValue = model_auto.VC_CITY;

            DropDownList4.DataTextField = "AreaName";
            DropDownList4.DataValueField = "AreaId";
            DropDownList4.DataSource = new BLL.Area().GetAllList(model_auto.VC_CITY);
            DropDownList4.DataBind();

            //model_auto.VC_AREA = model_auto.VC_AREA == string.Empty ? v_customer_address.Tables[1].Rows[0]["Area"].ToString() : model_auto.VC_AREA;
            DropDownList4.SelectedValue = model_auto.VC_AREA;
        }
        catch { }


        try
        {
            DropDownList1.SelectedValue = model_auto.CertiFicateType.ToString();
        }
        catch {
            DropDownList1.SelectedValue = "0";
        }
        Label2.Text = model_customer.BYZD5;





    }

    private void Bind1()
    {

        // List<Model.Province> citys = new List<Model.Area>();
        BLL.Province bll = new BLL.Province();
        DropDownList2.DataTextField = "ProName";
        DropDownList2.DataValueField = "ProId";
        DropDownList2.DataSource = bll.GetAllList();
        DropDownList2.DataBind();

    }

    private void Bind2(string pid)
    {

        BLL.City bll = new BLL.City();
        DropDownList3.DataTextField = "CityName";
        DropDownList3.DataValueField = "CityId";
        DropDownList3.DataSource = bll.GetAllList(pid);
        DropDownList3.DataBind();
    }

    private void Bind3(string cid)
    {
        BLL.Area bll = new BLL.Area();
        DropDownList4.DataTextField = "AreaName";
        DropDownList4.DataValueField = "AreaId";
        DropDownList4.DataSource = bll.GetAllList(cid);
        DropDownList4.DataBind();


    }


     



    protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind2(DropDownList2.SelectedValue);
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        Bind3(DropDownList3.SelectedValue);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {


        LX_Manage.ERP.V_CUSTOMER_AUTO erp_auto = new LX_Manage.ERP.V_CUSTOMER_AUTO();
        Model.V_CUSTOMER_AUTO model_auto = new BLL.V_CUSTOMER_AUTO().GetModel(usercode);



        LX_Manage.ERP.V_CUSTOMER erp_customer = new LX_Manage.ERP.V_CUSTOMER();
        Model.V_CUSTOMER model_customer = new BLL.V_CUSTOMER().GetModel(usercode);



        if (RadioButton1.Checked)
        {
            model_customer.SEX = "0";
        }
        else
        {
            model_customer.SEX = "1";
        }

        model_customer.GKMC = TextBox1.Text;


        model_auto.CertiFicateType = Convert.ToInt32(DropDownList1.SelectedValue);

        model_auto.CardNO = TextBox2.Text;

        model_customer.SR = Convert.ToDateTime(TextBox10.Text);
        model_customer.YB = HasPermission ? TextBox3.Text : model_customer.YB;
        model_customer.SJ = HasPermission ? TextBox4.Text : model_customer.SJ;


        //  qq,msn ,旺旺号
        model_customer.DZ = HasPermission ? txtAddress.Text : model_customer.DZ;
        model_customer.EMAIL = HasPermission ? txtEmail.Text : model_customer.EMAIL;
        model_customer.BZ = txtBZ.Text;

        model_customer.DH = HasPermission ? TextBox5.Text + "-" + TextBox6.Text+"-"+TextBox11.Text : model_customer.DH;
        model_auto.BYZD18 = HasPermission ? TextBox7.Text : model_auto.BYZD18;
        model_auto.BYZD16 = HasPermission ? TextBox8.Text : model_auto.BYZD16;
        model_auto.BYZD17 = HasPermission ? TextBox9.Text : model_auto.BYZD17;


        model_auto.VC_PROVINCE = DropDownList2.SelectedValue;

        model_auto.VC_CITY = DropDownList3.SelectedValue;

        model_auto.VC_AREA = DropDownList4.SelectedValue;


      //  new BLL.V_CUSTOMER_AUTO().Update(model_auto);
        //   new BLL.V_CUSTOMER().Update(model_customer);



        #region

        erp_customer.AGE = model_customer.AGE;
        erp_customer.BM = model_customer.BM;
        erp_customer.BYZD1 = model_customer.BYZD1;
        erp_customer.BYZD10 = model_customer.BYZD10;
        erp_customer.BYZD11 = model_customer.BYZD11;
        erp_customer.BYZD12 = model_customer.BYZD12;
        erp_customer.BYZD13 = model_customer.BYZD13;
        erp_customer.BYZD14 = model_customer.BYZD14;
        erp_customer.BYZD15 = model_customer.BYZD15;
        erp_customer.BYZD2 = model_customer.BYZD2;
        erp_customer.BYZD3 = model_customer.BYZD3;
        erp_customer.BYZD4 = model_customer.BYZD4;
        erp_customer.BYZD5 = model_customer.BYZD5;
        erp_customer.BYZD6 = model_customer.BYZD6;
        erp_customer.BYZD7 = model_customer.BYZD7;
        erp_customer.BYZD8 = model_customer.BYZD8;
        erp_customer.BYZD9 = model_customer.BYZD9;
        erp_customer.BZ = model_customer.BZ;
        erp_customer.CITY = model_customer.CITY;
        erp_customer.CKDM = model_customer.CKDM;
        erp_customer.COUNTRY = model_customer.COUNTRY;
        erp_customer.CZ = model_customer.CZ;
        erp_customer.DH = model_customer.DH;
        erp_customer.DISTRICT = model_customer.DISTRICT;
        erp_customer.DM = model_customer.DM;
        erp_customer.DQJF = model_customer.DQJF;
        erp_customer.DUDS = model_customer.DUDS;
        erp_customer.DZ = model_customer.DZ;
        erp_customer.EARNING = model_customer.EARNING;
        erp_customer.EDUCATION = model_customer.EDUCATION;
        erp_customer.EMAIL = model_customer.EMAIL;
        erp_customer.FJSX1 = model_customer.FJSX1;
        erp_customer.FJSX2 = model_customer.FJSX2;
        erp_customer.FJSX3 = model_customer.FJSX3;
        erp_customer.FJSX4 = model_customer.FJSX4;
        erp_customer.FJSX5 = model_customer.FJSX5;
        erp_customer.FJSX6 = model_customer.FJSX6;
        erp_customer.FKBJ = model_customer.FKBJ;
        erp_customer.GKMC = model_customer.GKMC;
        erp_customer.JDRQ = model_customer.JDRQ;
        erp_customer.KITHMC = model_customer.KITHMC;
        erp_customer.KITHSEX = model_customer.KITHSEX;
        erp_customer.KNOWS = model_customer.KNOWS;
        erp_customer.MARRY = model_customer.MARRY;
        erp_customer.OCCUPATION = model_customer.OCCUPATION;
        erp_customer.QDDM = model_customer.QDDM;
        erp_customer.QYDM = model_customer.QYDM;
        erp_customer.SEX = model_customer.SEX;
        erp_customer.SJ = model_customer.SJ;
        erp_customer.SR = model_customer.SR;
        erp_customer.SZGG = model_customer.SZGG;
        erp_customer.TKBJ = model_customer.TKBJ;
        erp_customer.VERSION = model_customer.VERSION;
        erp_customer.WORTH = model_customer.WORTH;
        erp_customer.XFJE = model_customer.XFJE;
        erp_customer.XFSL = model_customer.XFSL;
        erp_customer.XGRQ = model_customer.XGRQ;
        erp_customer.XZGG = model_customer.XZGG;
        erp_customer.YB = model_customer.YB;

        erp_auto.AddressType = model_auto.AddressType;
        erp_auto.BYZD1 = model_auto.BYZD1;
        erp_auto.BYZD10 = model_auto.BYZD10;
        erp_auto.BYZD11 = model_auto.BYZD11;
        erp_auto.BYZD12 = model_auto.BYZD12;
        erp_auto.BYZD13 = model_auto.BYZD13;
        erp_auto.BYZD14 = model_auto.BYZD14;
        erp_auto.BYZD15 = model_auto.BYZD15;
        erp_auto.BYZD16 = HasPermission ? model_auto.BYZD16 : erp_auto.BYZD16;
        erp_auto.BYZD17 = HasPermission ? model_auto.BYZD17 : erp_auto.BYZD17;
        erp_auto.BYZD18 = HasPermission ? model_auto.BYZD18 : erp_auto.BYZD18;
        erp_auto.BYZD2 = model_auto.BYZD2;
        erp_auto.BYZD3 = model_auto.BYZD3;
        erp_auto.BYZD4 = model_auto.BYZD4;
        erp_auto.BYZD5 = model_auto.BYZD5;
        erp_auto.BYZD6 = model_auto.BYZD6;
        erp_auto.BYZD7 = model_auto.BYZD7;
        erp_auto.BYZD8 = model_auto.BYZD8;
        erp_auto.BYZD9 = model_auto.BYZD9;
        erp_auto.CardNO = model_auto.CardNO;
        erp_auto.CertiFicateType = model_auto.CertiFicateType;
        erp_auto.CompanyPhone = model_auto.CompanyPhone;
        erp_auto.CreatDateTime = model_auto.CreatDateTime;
        erp_auto.CustomerLevel=model_auto.CustomerLevel;
        erp_auto.PhoneMessage=model_auto.PhoneMessage;
        erp_auto.VC_ADDRESS=model_auto.VC_ADDRESS;
        erp_auto.VC_AREA=model_auto.VC_AREA;
        erp_auto.VC_CITY=model_auto.VC_CITY;
        erp_auto.VC_CODE=model_auto.VC_CODE;
        erp_auto.VC_LASTLOGINTIME=model_auto.VC_LASTLOGINTIME;
        erp_auto.VC_Note=model_auto.VC_Note;
        erp_auto.VC_PROVINCE=model_auto.VC_PROVINCE;
        erp_auto.VC_PWDANSWER=model_auto.VC_PWDANSWER;
        erp_auto.VC_PWDQUESTION=model_auto.VC_PWDQUESTION;


        #endregion



        string code = ConfigurationManager.AppSettings["CODE"];
        LX_Manage.ERP.Service erp = new LX_Manage.ERP.Service();

        string result = erp.Customer_Update(code, erp_customer, erp_auto); 





    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Model.V_CUSTOMER_AUTO model_auto = new BLL.V_CUSTOMER_AUTO().GetModel(usercode);
        model_auto.VC_STOP = 1;
        model_auto.VC_Note = model_auto.VC_Note + "(该成员已被列入黑名单)";

        if (new BLL.V_CUSTOMER_AUTO().Update(model_auto))
        {
            Share.Alert(this.Page, "操作成功"); return;
        }
        else
        { Share.Alert(this.Page, "操作失败"); return; }
        
    }
}
